<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <!-- 搜索建议区域 -->
    <view class="sugg-list" v-if="searchList.length !== 0">
      <view class="sugg-item" v-for="(item, index) in searchList" :key="index" @click="goGoodsDetail(item)">
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="17"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史区域 -->
    <view class="history-box" v-else>
      <!-- 标题区域 -->
      <view class="history-item">
        <text>搜索历史</text>
        <uni-icons type="trash-filled" size="17" @click="clearHistory"></uni-icons>
      </view>
      <!-- 标签区域 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, index) in histories" :key="index" @click="goGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null, // 防抖定时器
        keywords: '', //搜索关键词
        searchList: [], // 搜索建议列表
        historyList: [] // 搜索历史
      };
    },
    onLoad () {
      this.historyList = JSON.parse(uni.getStorageSync('KW') || '[]')
    },
    methods: {
      // 搜索框的输入事件
      input(e) {
        clearTimeout(this.timer)
        // 防抖
        this.timer = setTimeout(() => {
          this.keywords = e
          this.getSearchList()
        }, 500)
      },
      // 获取搜索建议
      async getSearchList() {
        // 判断是否为空
        if (this.keywords === '') {
          this.searchList = []
          return
        }
        // 发送网络请求
        const {
          data: res
        } = await uni.$http.get('/goods/qsearch', {
          query: this.keywords
        })
        // 判断
        if (res.meta.status !== 200) return uni.$showMsg()
        // 成功赋值
        this.searchList = res.message
        // 保存搜索历史
        this.saveSearchHistory()
      },
      // 搜索建议跳转
      goGoodsDetail(val) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + val.goods_id
        })
      },
      // 保存搜索历史
      saveSearchHistory() {
        // 将数据转换为 set 格式
        const set = new Set(this.histories)
        // 去除对应的关键词
        set.delete(this.keywords)
        // 添加对应的关键词
        set.add(this.keywords)
        // 存储数据
        this.historyList = Array.from(set)
        // 持久化存储
        uni.setStorageSync('KW', JSON.stringify(this.historyList))
      },
      // 清除搜索历史
      clearHistory() {
        // 清除搜索历史中的数据
        this.historyList = []
        // 清除本地存储
        uni.setStorageSync('KW', '[]')
      },
      // 搜索历史进行跳转
      goGoodsList(kw) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + kw
        })
      }
    },
    computed: {
      histories () {
        return [...this.historyList].reverse()
      }
    }
  }
</script>

<style lang="scss">
  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 13px 0;
      font-size: 14px;
      border-bottom: 1px solid #eee;

      .goods-name {
        // 文本不换行
        white-space: nowrap;
        // 溢出隐藏
        overflow: hidden;
        // 显示 ... 
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }
  .history-box{
    padding: 0 5px;
    .history-item{
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 13px;
      line-height: 40px;
      border-bottom: 1px solid #eee;
      .uni-icons{
        color: #aaa;
      }
    }
    .history-list {
      display: flex;
      flex-wrap: wrap;
      .uni-tag {
        // margin: 5px 8px;
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
